App({
  globalData: {
    audioCtx: wx.createInnerAudioContext(),
    // 播放状态
    state: 'paused',
    play: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      coverImgUrl: '/images/cover.jpg',
      playIndex: 0,
      playMusicId: null
    },
    playlist: [],
    // {
    //     id: 1,
    //     title: '夜曲',
    //     singer: '肖邦',
    //     src: `/music/1.mp3`,
    //     image: '/images/1.jpg'
    // }
  },
  onLaunch: function () {
    wx.request({
      url: 'http://www.mmdccj.top:9003/user/login/account',
      method: 'POST',
      data: {
        "account": "MMDCCJ",
        "password": "123456"
      },
      header: {
        'content-type': 'application/json'
      },
      fail: res => {
        console.log("登录失败");
      },
      success: res => {
        console.log(res);
        wx.setStorageSync('cookie', res.cookies[0].split(";")[0].split("=")[1])
      }
    })
    // 播放进度检测
    this.globalData.audioCtx.onError(function () {
      console.log('播放失败：' + that.audioCtx.src)
    })
    // 播放完成自动换下一曲
    this.globalData.audioCtx.onEnded(function () {
      console.log("播放完毕");
      that.next()
    })
    // 自动更新播放进度
    this.globalData.audioCtx.onPlay(function () {})
    let that = this
    // 监听播放进度于index/index.js中调用
    // this.globalData.audioCtx.onTimeUpdate()
  },
  // 格式化时间
  formatTime: function (time) {
    // console.log("====>orginal Time",time)

    var minute = Math.floor(time / 60) % 60;
    var second = Math.floor(time) % 60
    return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)

  },
  // 添加音乐 并播放 
  setMusicAndPlay: function (music, singerName) {
    console.log(1);
    this.globalData.audioCtx.src = "http://www.mmdccj.top:9003/static/music/" + music.src
    this.globalData.playlist.push({
      id: music.musicId,
      title: music.title,
      singer: singerName,
      src: "http://www.mmdccj.top:9003/static/music/" + music.src,
      image: "http://www.mmdccj.top:9003/static/image/" + music.image
    })
    this.globalData.state = 'playing'
    this.globalData.play = {
      'playIndex': this.globalData.playlist.length - 1,
      'playMusicId': music.musicId,
      'title': music.title,
      'singer': singerName,
      'coverImgUrl': "http://www.mmdccj.top:9003/static/image/" + music.image,
      'currentTime': '00:00',
      'duration': '00:00',
      'percent': 0
    }
    this.globalData.audioCtx.onCanplay(() => {
      this.globalData.audioCtx.play()
    })
  },
  pushMusicToList: function (music, singerName) {
    if (this.globalData.playlist.length == 0) {
      this.globalData.playlist.push({
        id: music.musicId,
        title: music.title,
        singer: singerName,
        src: "http://www.mmdccj.top:9003/static/music/" + music.src,
        image: "http://www.mmdccj.top:9003/static/image/" + music.image
      })
    } else {
      const list_tmp = this.globalData.playlist
      for (const music_ of list_tmp) {
        if (music_.id == music.musicId) {
          return;
        }
      }
      this.globalData.playlist.push({
        id: music.musicId,
        title: music.title,
        singer: singerName,
        src: "http://www.mmdccj.top:9003/static/music/" + music.src,
        image: "http://www.mmdccj.top:9003/static/image/" + music.image
      })
    }
    if (this.globalData.playlist.length == 1) {
      this.globalData.audioCtx.src = "http://www.mmdccj.top:9003/static/music/" + music.src
      this.globalData.play = {
        'playIndex': 0,
        'playMusicId': music.musicId,
        'title': music.title,
        'singer': singerName,
        'coverImgUrl': "http://www.mmdccj.top:9003/static/image/" + music.image,
        'currentTime': '00:00',
        'duration': '00:00',
        'percent': 0
      }
    }

  },
  // 切换到指定歌曲
  updateMusic: function (index) {
    let playList = this.globalData.playlist
    this.globalData.audioCtx.src = playList[index].src
    this.globalData.play = {
      'playIndex': index,
      'playMusicId': playList[index].id,
      'title': playList[index].title,
      'singer': playList[index].singer,
      'coverImgUrl': playList[index].image,
      'currentTime': '00:00',
      'duration': '00:00',
      'percent': 0
    }
  },
  // 播放按钮
  play: function () {
    this.globalData.state = 'playing'
    this.globalData.audioCtx.pause()
    this.globalData.audioCtx.play()
  },

  // 暂停按钮
  pause: function () {
    this.globalData.state = 'paused'
    this.globalData.audioCtx.pause()
  },

  // 下一曲按钮
  next: function () {
    if (this.globalData.playlist.length > this.globalData.play.playIndex + 1) {
      this.globalData.play.playIndex += 1;
      this.updateMusic(this.globalData.play.playIndex);
    } else if (this.globalData.playlist.length == 1) {
      wx.hideLoading()
      return
    } else {
      if (this.globalData.play.playIndex == 0) {
        wx.hideLoading()
        return
      }
      this.globalData.play.playIndex = 0
      this.updateMusic(0);
    }
    this.globalData.audioCtx.onCanplay(() => {
      this.play()
      wx.hideLoading()
      this.state = "playing"
    })
  },

  // 滚动条调节歌曲进度
  sliderChange: function (e) {
    var second = e.detail.value * this.globalData.audioCtx.duration / 100
    console.log("跳转到", second);
    this.globalData.audioCtx.seek(second)
    if (this.globalData.state == 'paused') {
      this.globalData.audioCtx.onCanplay(() => {
        this.globalData.audioCtx.play()
        this.globalData.state = 'playing'
      })
    } else {
      this.globalData.audioCtx.pause()
      this.globalData.audioCtx.onCanplay(() => {
        this.globalData.audioCtx.play()
        this.globalData.state = 'playing'
      })
    }
  },
  popMusic: function (index) {
    if (this.globalData.playlist.length == 1) {
      this.globalData.playlist.pop()
      this.globalData.audioCtx.stop()
      this.playReset()
    } else if (this.globalData.playlist.length == 0) {
      return;
    }
    // 移除的单曲在playIndex前
    if (this.globalData.play.playIndex > index) {
      this.globalData.playlist.splice(index, 1)
      this.globalData.play.playIndex--;
    } else if (this.globalData.play.playIndex < index) {
      // 移除的单曲在playIndex后
      this.globalData.playlist.splice(index, 1);
    } else if (this.globalData.play.playIndex == index) {
      this.globalData.playlist.splice(index, 1);
      this.globalData.play.playIndex--;
      this.globalData.audioCtx.stop()
      this.updateMusic(this.globalData.play.playIndex)
      if (this.globalData.state == 'playing') {
        this.globalData.audioCtx.onCanplay(() => {
          this.globalData.audioCtx.play()
        })
      }
    }
    console.log("pop完成后", this.globalData.playlist);

  },
  playReset: function () {
    this.globalData.play.title = ""
    this.globalData.play.singer = ""
    this.globalData.play.coverImgUrl = '/images/cover.jpg'
    this.globalData.state = "paused"
  }
})